<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CnshipController
 *
 * @author wupeng
 */
class Admini_CnshipController extends Uuch_ManageController {

    //put your code here

    public function init() {
        parent::init();
        $this->appendCss('/admins/css/common.css');
        $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js');
        $this->setTitle("国内物流管理");
        $this->view->type = "cnship";
    }

    public function indexAction() {
        $pagesize = 15;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $quick = $this->getSafeParam("quick");
        $isDisable = $this->getSafeParam("disabled");
        $cnshipLists = Application_Model_shipType::getFilteds($quick, $isDisable, $pagesize, $pageindex);
        $this->view->cnshiplists = $cnshipLists['list'];
        $this->view->cnshiplistsAll = $cnshipLists;
        $this->view->quick = $quick;
        $this->view->ma = $this->getSafeParam('ma');
        $this->setTitle("国内物流管理");
    }

    public function addAction() {
        if ($this->isPost()) {
            $name = $this->getSafeParam('name');
            if(Application_Model_shipType::getByName($name)){
                $this->_redirect('/admini/cnship/index/ma/2');
                return;
            }
            $shipType = new Application_Model_shipType();
            $shipType->name = $name;
            $shipType->type = Application_Model_shipType::TYPE_CN_SHIP;
            $shipType->isUrl = 0;
            $shipType->disabled = 1;
            $shipType->createdAt = date('Y-m-d H:i:s');
            $shipType->code = $this->getSafeParam('code');
            $shipType->comment = $this->getSafeParam('comment');
            $shipType->word = $this->getSafeParam('word');
            try {
                $shipType->save();
                Application_Model_Operate::addOperate(0, null, $shipType->id, "添加国内物流：".$name, 0);
                $this->_redirect('/admini/cnship/index/ma/1');
            } catch (Exception $e) {
                $this->_redirect('/admini/cnship/index/ma/0');
            }
        }
        $this->_redirect('/admini/cnship/index/ma/0');
    }

    public function deleteAction() {
        $id = $this->getSafeParam('id');
        if ($id) {
            $shipType = new Application_Model_shipType();
            $shipType->id = $id;
            try {
                $shipType->delete();
                $this->json(1);
            } catch (Exception $ex) {
                $this->json(0);
            }
        }else{
            $this->json(0);
        }
        
    }

    public function shipdetailAction() {
        $this->setTitle("快递详情");
        $this->appendCss('/admins/css/adminshare.css');
        $this->appendCss('/admins/css/cangku.css');
        $id = $this->getSafeParam("id");
        $shipType = Application_Model_shipType::load($id);
        if ($shipType) {
            if ($this->isPost()) {
                $shipType->name = $this->getSafeParam('name');
                $shipType->code = $this->getSafeParam('code');
                $shipType->word = $this->getSafeParam('word');
                $shipType->comment = $this->getSafeParam('comment');
                $shipType->save();
                $this->view->ma = 1;
            }
        }
        $this->view->isEdit = $this->getSafeParam("isEdit");
        $this->view->shipType = $shipType;
    }

}
